package com.yanqu.road.server.logic.activity.relation.condition;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.activity.chefDuel.eChefDuelActivityType;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.event.EventTriggerArgs;
import com.yanqu.road.entity.event.IEventListener;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.RelationActivityModule;
import com.yanqu.road.server.logic.activity.relation.RelationActivityData;
import com.yanqu.road.server.logic.activity.relation.condition.chefduel.*;
import com.yanqu.road.server.logic.activity.relation.condition.union.CookBoy1CrossScoreUnionRankCondition;

import java.math.BigInteger;

public abstract class BaseRelationActivityCondition {
    protected GamePlayer player;
    protected RelationActivityData activityData;
    protected ActivityConditionInfo conditionInfo;

    public BaseRelationActivityCondition(GamePlayer player, RelationActivityData activityData, ActivityConditionInfo conditionInfo) {
        this.player = player;
        this.activityData = activityData;
        this.conditionInfo = conditionInfo;
    }

    public abstract void addTrigger(GamePlayer player);

    public abstract void removeTrigger(GamePlayer player);

    public abstract boolean isCompleted(GamePlayer player);

    //这里的condition会常驻内存，别让策划配太多了
    public static BaseRelationActivityCondition createActivityCondition(GamePlayer player, RelationActivityData activityData, ActivityInfo activityInfo, ActivityConditionInfo conditionInfo) {
        //对应写好各种活动对应的condition类型
        if(activityInfo.getType() == eActivityType.CookingBoyActivity.getValue()) {
            if (activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()) {
                if (conditionInfo.getType() == eGamePlayerEventType.CookBoy1IntegralUserCrossRank.getValue()) {//小当家个人排行榜
                    return new CookBoy1CrossScoreRankCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy1IntegralUnionCrossRank.getValue()) {//小当家商会排行榜
                    return new CookBoy1CrossScoreUnionRankCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy1MakeFoodFinishCount.getValue()) {//小当家完成做几道菜任务
                    return new CookBoy1MakeFoodTimesCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy1IntegralServerCrossRank.getValue()) {//小当家区服榜
                    return new CookBoy1ServerScoreRankCondition(player, activityData, conditionInfo);
                }
            } else if (activityInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()) {
                if (conditionInfo.getType() == eGamePlayerEventType.CookBoy2IntegralUserCrossRank.getValue()) {
                    return new CookBoy2CrossScoreRankCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy2MakeDishCount.getValue()) {
                    return new CookBoy2MakeDishTimesCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy2MakeDishUnLock.getValue()) {
                    return new CookBoy2MakeDishUnlockCondition(player, activityData, conditionInfo);
                }
            } else if (activityInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()) {
                if (conditionInfo.getType() == eGamePlayerEventType.CookBoy3IntegralUserCrossRank.getValue()) {
                    return new CookBoy3CrossScoreRankCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy3OpenBanquetTimes.getValue()) {
                    return new CookBoy3OpenBanquetTimesCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy3AllBanquetUserCount.getValue()) {
                    return new CookBoy3AllBanquetIntegralCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.CookBoy3BanquetOpenTimes.getValue()) {
                    return new CookBoy3BanquetOpenTimesCondition(player, activityData, conditionInfo);
                }
            }
        } else if(activityInfo.getType() == eActivityType.ChefDuel.getValue()) {
            if (activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()) {
//                if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel1IntegralUserCrossRank.getValue()) {//小当家个人排行榜
//                    return new ChefDuel1CrossScoreRankCondition(player, activityData, conditionInfo);
//                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel1IntegralUnionCrossRank.getValue()) {//小当家商会排行榜
//                    return new ChefDuel1CrossScoreUnionRankCondition(player, activityData, conditionInfo);
//                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel1MakeFoodFinishCount.getValue()) {//小当家完成做几道菜任务
//                    return new ChefDuel1MakeFoodTimesCondition(player, activityData, conditionInfo);
//                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel1IntegralServerCrossRank.getValue()) {//小当家区服榜
//                    return new ChefDuel1ServerScoreRankCondition(player, activityData, conditionInfo);
//                }
            } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()) {
                if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel2IntegralUserCrossRank.getValue()) {
                    return new ChefDuel2CrossScoreRankCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel2MakeDishCount.getValue()) {
                    return new ChefDuel2MakeDishTimesCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel2MakeDishUnLock.getValue()) {
                    return new ChefDuel2MakeDishUnlockCondition(player, activityData, conditionInfo);
                }
            } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()) {
                if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel3IntegralUserCrossRank.getValue()) {
                    return new ChefDuel3CrossScoreRankCondition(player, activityData, conditionInfo);
//                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel3OpenBanquetTimes.getValue()) {
//                    return new ChefDuel3OpenBanquetTimesCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel3AllBanquetUserCount.getValue()) {
                    return new ChefDuel3AllBanquetIntegralCondition(player, activityData, conditionInfo);
                } else if (conditionInfo.getType() == eGamePlayerEventType.ChefDuel3BanquetOpenTimes.getValue()) {
                    return new ChefDuel3BanquetOpenTimesCondition(player, activityData, conditionInfo);
                }
            }
        }
        return null;
    }

    public final void setValue(BigInteger value) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setValue(value);
        }
        player.getModule(RelationActivityModule.class).onActivityDataChanged(activityData);
    }

    public  BigInteger getPreValue() {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            return userActivityConditionData.getPreValue();
        }else {
            return BigInteger.ZERO;
        }
    }

    public final BigInteger getValue() {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            return userActivityConditionData.getValue();
        }
        return BigInteger.ZERO;
    }

    private UserActivityConditionData getUserActivityConditionData(int conditionId){
        for (UserActivityConditionData userActivityConditionData : activityData.getUserActivityConditionDataList()) {
            if (userActivityConditionData.getConditionId() == conditionId) {
                return userActivityConditionData;
            }
        }
        return null;
    }

    protected abstract void player_BoardLevelHandle(Object object);

    protected final player_BoardLevelHandle handle = new player_BoardLevelHandle();

    public ActivityConditionInfo getConditionInfo() {
        return conditionInfo;
    }

    public void setPreValue(BigInteger value) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setPreValue(value);
        }
    }

    public void initDefaultValue(){

    }

    public  void setGetReward(boolean isGetReward){
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setGetReward(isGetReward);
        }
    }

    public void setModifyTime(long modifyTime) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if(null != userActivityConditionData){
            userActivityConditionData.setModifyTime(modifyTime);
        }
    }

    public void initValue() {
        if (activityData.isInTime()) {
            if(getValue().compareTo(BigInteger.ZERO) == 0) {
                initDefaultValue();
            }
        }
    }

    public class player_BoardLevelHandle implements IEventListener {
        @Override
        public void triggerEvent(EventTriggerArgs eventArgs) {
            changeValue(eventArgs.getArgs());
        }
    }

    public void changeValue(Object object) {
        if (activityData.isShowTime()) {// 活动期间改变值
            player_BoardLevelHandle(object);
        }
    }

    public int getConditionId() {
        return conditionInfo.getConditionId();
    }

    public int getConditionType() {
        return conditionInfo.getType();
    }
}
